package com.acme.bankapp.domain.bank;

public class Client {

    public String fio;
    private Gender gender;
    public SavingAccount savingAccount;
    public CheckingAccount checkingAccount;

    public Client(String fio, Gender gend, double savingAccount) {
        this.fio = fio;
        this.gender = gend;
        this.savingAccount = new SavingAccount(savingAccount);
    }

    public Client(String fio, Gender gend, double chacc1, double chacc2) {
        this.fio = fio;
        this.gender = gend;
        this.checkingAccount = new CheckingAccount(chacc1, chacc2);
    }

    public Client(String fio, Gender gend, double savingAccount, double chacc1, double chacc2) {
        this.fio = fio;
        this.gender = gend;
        this.savingAccount = new SavingAccount(savingAccount);
        this.checkingAccount = new CheckingAccount(chacc1, chacc2);
    }


    public String getClientSalutation() {
        String tmp = "";

        switch (this.gender) {
            case Male:
                tmp = "Mr. " + this.fio;
                break;
            case Female:
                tmp = "Ms. " + this.fio;
                break;
        }
        return tmp;
    }

    @Override
    public int hashCode() {
        return (17 * (this.fio.hashCode() + this.gender.hashCode()));
    }

    @Override
   /*
    * The method takes into account the class constructors
    *
    */
    public boolean equals(Object c) {
        if (c == null)
            return false;
        if (!(c instanceof Client))
            return false;
        Client compareClient = (Client) c;
        if (!this.fio.equals(compareClient.fio))
            return false;
        if (this.gender != compareClient.gender)
            return false;
        if (this.savingAccount == null && compareClient.savingAccount == null) {
            if (this.checkingAccount != null && compareClient.checkingAccount != null) {
                if (this.checkingAccount.balance == compareClient.checkingAccount.balance &&
                        this.checkingAccount.getOverdraft() == compareClient.checkingAccount.getOverdraft()) {
                    return true;
                } else return false;
            }
        }
        else if (this.savingAccount != null && compareClient.savingAccount != null) {
            if (this.savingAccount.balance == compareClient.savingAccount.balance) {
                if (this.checkingAccount != null && compareClient.checkingAccount != null) {
                    if (this.checkingAccount.balance == compareClient.checkingAccount.balance &&
                            this.checkingAccount.getOverdraft() == compareClient.checkingAccount.getOverdraft()) {
                        return true;
                    }
                } else {
                    if (this.checkingAccount == null && compareClient.checkingAccount == null) return true;
                    else return false;
                }
            }
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuffer tmp = new StringBuffer();
        tmp.append(getClientSalutation() + "\n");

        if (checkingAccount != null)
            tmp.append(checkingAccount.toString());
        if (savingAccount != null)
            tmp.append(savingAccount.toString());

        return tmp.toString();
    }
}